/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilica.cisteniulic.listener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import net.hnilica.cisteniulic.emailer.TermChecker;

/**
 *
 * @author swosh
 */
public class EmailListener implements ServletContextListener {

    private ServletContext context = null;
    private static final int day = 24 * 60 * 60 * 1000;
    //private static final TermChecker emailChecker = TermChecker.getTermChecker();

    private final Timer timer = new Timer();

    private final SimpleDateFormat pattern = new SimpleDateFormat("yyyy.MM.dd G HH:mm:ss Z");
    private final String startTimeString = pattern.format(new Date(new Date().getTime() - day));
    private Date startTime;
    private final TermChecker termChecker = TermChecker.getTermChecker();

    public void contextInitialized(ServletContextEvent event) {
        //context = event.getServletContext();
        try {
            startTime = pattern.parse(startTimeString); //should at five
        } catch (ParseException ex) {
            Logger.getLogger(EmailListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        timer.scheduleAtFixedRate(termChecker, startTime, day);
    }

    public void contextDestroyed(ServletContextEvent event) {
        //context = null;
    }

}
